/**
 * 物体基础类
 * 
 * 质量
 * 尺寸
 */

package com.struct.graph.game.body
{
	import com.component.ComponentChildren;
	import com.utils.Collide;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	
	public class BaseBody extends ComponentChildren
	{
		
		protected var _collide:Collide;
		protected var bitMap:Bitmap;
		private var _radius:int;
		private var isMove:Boolean;
		private var isHarm:Boolean;
		public var move:int;
		protected var attack:int, defense:int, quality:Number, damp:Number, left:int;
		public function BaseBody(radius:int, left:int, attack:int, defense:int, quality:Number, bitmapData:BitmapData, width:Number, height:Number, damp:Number = 0.02, isHarm:Boolean = true, isMove:Boolean = true)
		{
			super();
			_collide = new Collide();
			this._radius = radius;
			this.width = bitmapData.width;
			this.height = bitmapData.height;
			this.left = left;
			
			this.attack = attack;
			this.defense = defense;
			_collide.quality = this.quality = quality;
			this.damp = damp;
			_collide.attack = 1;
			
			this.isHarm = isHarm;
			this.isMove = isMove;
			//初始化bitmap
			bitMap = new Bitmap(bitmapData);
			bitMap.x = -width/2;
			bitMap.y = -height /2;
			addChild(bitMap);
			
			graphics.beginFill(0x00);
			graphics.drawCircle(0, 0, radius);
			graphics.endFill();
		}
		/**
		 * 范围
		 */
		public function get radius():int{
			return _radius;
		}
		/**
		 * 检测碰撞消耗
		 */
		public function validtorCollide2Destroy(collide:Collide):Boolean{
			if(isHarm){
				var harm:int = Math.max(collide.attack - defense / 2, 1);
				move = Math.max(0, collide.move + move);
				left = Math.max(0, left - harm);
				return left == 0;
			}
			return false;
		}
		
		/**
		 * 碰撞系数
		 * 
		 */
		public function get collide():Collide{
			return _collide;
		}
		
		/**
		 * 移动前的参数设置。
		 * @param angle 要移动的角度
		 * @param speed 移动注入的速度比例
		 */
		public function fire(angle:Number, speed:Number):void{
			if(isMove){
				_collide.speed = speed;
				_collide.angle = angle;
			} else {
				stop();
			}
		}
		
		public function execution():Boolean{
			if(_collide.speed > 0 && isMove){
				x += _collide.speedx;
				y += _collide.speedy;
				//每次execution, 都移动speed距离, 能量守恒算出终速度
				_collide.speed = Math.sqrt(_collide.speed*_collide.speed - 2*damp* 9.8*_collide.speed);
				return true;
			} else {
				stop();
				return false;
			}
		}
		
		public function stop():void{
			_collide.speed = 0;
		}
	}
}